﻿using System;
using System.IO;
using System.Net;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Diagnostics;



namespace Scrobbler
{
    internal class CScrobbler:IScrobbler
    {

        

        private string m_User;
        private string m_Password;
        private string m_ClientID;
        private string m_ClientVersion;



        private string GetAutenticationToken(string timestamp)
        {
            string token;
            string passwordhash;

            passwordhash = this.GetMD5Hash(m_Password) + timestamp;
            token = this.GetMD5Hash(passwordhash);

            return token.ToLower();
        }



        private string GetMD5Hash(string value)
        {
            MD5 md5;

            md5 = new MD5CryptoServiceProvider();
           
            byte[] passwordHash = md5.ComputeHash(Encoding.ASCII.GetBytes(value));
            return BitConverter.ToString(passwordHash).Replace("-", "").ToLower();

        }



        #region IScrobbler Members

        public void SetUser(string user)
        {
            m_User = HttpUtility.UrlEncode(user, Encoding.UTF8);
        }



        public void SetPassword(string password)
        {
            m_Password = HttpUtility.UrlEncode(password, Encoding.UTF8);
        }



        public string User
        {
            get { return m_User; }
        }

        public string Password
        {
            get { return m_Password; }
        }



        public void SetClientID(string id)
        {
            m_ClientID = HttpUtility.UrlEncode(id, Encoding.UTF8);
        }



        public void SetClientVersion(string version)
        {
            m_ClientVersion = HttpUtility.UrlEncode(version, Encoding.UTF8);
        }



        public List<string> Handshake()
        {
            try
            {
                string ResponseString;
                string[] ResponseStrings;
                string timestamp = CTime.GetUnixTime();
                string AuthToken = this.GetAutenticationToken(timestamp);

                string values = "?hs=" + HttpUtility.UrlEncode("true",Encoding.UTF8) + 
                                "&p=" + HttpUtility.UrlEncode("1.2.1",Encoding.UTF8) + 
                                "&c=" + m_ClientID + 
                                @"&v=" + m_ClientVersion + 
                                @"&u=" + m_User + 
                                @"&t=" + timestamp + 
                                @"&a=" + AuthToken;
                string url =
                @"http://post.audioscrobbler.com:80/" + values;

                List<string> ResponseArray = new List<string>();
                WebRequest request = WebRequest.Create(url);
               
                HttpWebResponse WebResponse = (HttpWebResponse)request.GetResponse();
                StreamReader rd = new StreamReader(WebResponse.GetResponseStream());

                ResponseString = rd.ReadToEnd();
                Debug.WriteLine(ResponseString);
                rd.Close();
                WebResponse.Close();

                ResponseStrings = ResponseString.Split('\n');

                foreach (string line in ResponseStrings)
                {
                    ResponseArray.Add(line.Replace("\n", ""));
                }

                CHandshakeStatus.IsHandshakeOk = true;

                return ResponseArray;
            }
            catch (Exception ex)
            {
                CHandshakeStatus.IsHandshakeOk = false;
                throw new Exception(ex.Message);
            }
        }

        #endregion


    }
}
